/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.odsKartW.web;
import cz.muni.fi.pb138.odsKartW.*;
import cz.muni.fi.pb138.odsKartW.MediaManager;
import cz.muni.fi.pb138.odsKartW.MediaManagerImpl;
import cz.muni.fi.pb138.odsKartW.MediaSource;
import cz.muni.fi.pb138.odsKartW.Medium;
import cz.muni.fi.pb138.odsKartW.ODSKartWException;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "ShowMedia", urlPatterns = {"/ShowMedia"})
public class ShowMedia extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ShowMedia</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ShowMedia at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        
        setDataShow(request);
        
        String submit = request.getParameter("submit");
        if(submit.equals("Zobraziť")) {
            request.setAttribute("show", true);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("create.jsp").forward(request, response);
        }
    }
    
    public void setDataShow(HttpServletRequest request) {
        
        String category = request.getParameter("categoryTitle");
        
        MediaManager mediaManager = null;
        try {
            MediaSource source = new MediaSource();
          //  mediaManager = new MediaManagerImpl(new File("http://code.google.com/p/ods-kart-w/source/browse/trunk/ODS/filmy.ods"));
            mediaManager = new MediaManagerImpl(new File(request.getSession().getServletContext().getRealPath(source.getSource())));
        } catch (IOException ex) {
            request.setAttribute("error", "Error: "+ex.getMessage());
        } //  catch (URISyntaxException ex) {
//            request.setAttribute("error", "Error: "+ex.getMessage());
//        }
        
        try {
            request.setAttribute("header1", mediaManager.getHeader(category));
        } catch (Exception ex) {
            Logger.getLogger(MediaManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("error", "Error: "+ex.getMessage() +category);
        }
        
        request.setAttribute("categoryTitle", category);
        
        List<Medium> media = new ArrayList<Medium>();
        try {
            media = new ArrayList<Medium>(mediaManager.findAllMedia(category));
            request.setAttribute("media", media);
        } catch (ODSKartWException ex) {
            Logger.getLogger(ShowMedia.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("error", "Error: "+ex.getMessage() + category );
        }
        
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
